package tt.com.datamanager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import tt.com.common.model.Car;

import tt.com.common.model.CremateStove;
import tt.com.common.util.PageUtil;
import tt.com.datamanager.mapper.CarMappers;
import tt.com.datamanager.service.CarService;

import java.util.Date;

@Service("carServiceImpls")
public class CarServiceImpl extends ServiceImpl<CarMappers,Car> implements CarService {
    @Autowired
    private CarMappers carMappers;

    @Override
    public Integer insertCar(Car car) {
        QueryWrapper<Car> wrapper = new QueryWrapper<>();
        wrapper.eq("car_num",car.getCarNum());
        if (baseMapper.selectOne(wrapper) != null) {
            // 重复添加返回-1
            return -1;
        }
        // 设置创建时间
        car.setCreateTime(new Date());
        car.setDataStatus(0);
        return baseMapper.insert(car);


    }

    @Override
    public PageUtil selectAllCarByPage(PageUtil pageUtil) {
        Page<Car> page = new Page<>(pageUtil.getPageIndex(), pageUtil.getPageSize());
        Page<Car> carPage = baseMapper.selectPage(page, null);
        // 设置总条数 自动计算总页数
        pageUtil.setTotalSize((int) carPage.getTotal());
        pageUtil.setData(carPage);
        return pageUtil;
    }

    @Override
    public Integer updateCar(Car car, String updateUserId) {
        // 设置修改时间
        car.setLastUpdateTime(new Date());
        // 设置修改操作人id
        car.setLastUpdateUserId(updateUserId);
        return baseMapper.updateById(car);
    }

    @Override
    public Integer deleteCar(Long id, String deleteUserId) {
        //根据id获取车辆
        Car car = baseMapper.selectById(id);
        if (car == null){
            //若数据不存在，返回-1
            return -1;
        }
        if (car.getDataStatus() == 1){
            //判断是否已经被删除过
            return -2;
        }
        //设置删除时间
        car.setDeleteTime(new Date());
        //设置删除操作人id
        car.setDeleteUserId(deleteUserId);
        car.setDataStatus(1);
        baseMapper.updateById(car);
        return baseMapper.deleteById(car.getId());
    }
}
